#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <windows.h>
#include <openssl/aes.h>

std::vector<unsigned char> readFile(const std::string& filename) {
    std::ifstream file(filename, std::ios::binary);
    return std::vector<unsigned char>((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
}

std::vector<unsigned char> decrypt(const std::vector<unsigned char>& data, const std::string& key) {
    std::vector<unsigned char> iv(data.begin(), data.begin() + AES_BLOCK_SIZE);
    
    AES_KEY aes_key;
    AES_set_decrypt_key(reinterpret_cast<const unsigned char*>(key.data()), 128, &aes_key);

    std::vector<unsigned char> decrypted(data.size() - AES_BLOCK_SIZE);
    AES_cbc_encrypt(data.data() + AES_BLOCK_SIZE, decrypted.data(), data.size() - AES_BLOCK_SIZE, &aes_key, iv.data(), AES_DECRYPT);

    return decrypted;
}

int main() {
    std::string filename = "encrypted.exe";
    std::string key;
    std::cout << "Enter the decryption key: ";
    std::getline(std::cin, key);

    std::vector<unsigned char> encrypted_data = readFile(filename);
    std::vector<unsigned char> decrypted_data = decrypt(encrypted_data, key);

    std::string temp_filename = "decrypted_temp.exe";
    std::ofstream temp_file(temp_filename, std::ios::binary);
    temp_file.write(reinterpret_cast<const char*>(decrypted_data.data()), decrypted_data.size());
    temp_file.close();

    STARTUPINFO si = {sizeof(si)};
    PROCESS_INFORMATION pi;
    if (CreateProcess(temp_filename.c_str(), NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
        WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
    } else {
        std::cerr << "Failed to run the decrypted executable." << std::endl;
    }

    DeleteFile(temp_filename.c_str());

    return 0;
}
